package com.jk.libsm.mapper;

import com.jk.libsm.pojo.User;
import com.jk.libsm.vo.Bookhistory;
import com.jk.libsm.vo.BorrowBook;
import com.jk.libsm.vo.Userinfo;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

/**
 * @author lk
 * @date 2020/7/6 0006 15:04
 * @description:
 */
@Mapper
public interface UserMappper {

    // 根据用户id查询用户密码
    @Select("select ReaderPassword from reader where RNo=#{id}")
    String getUserPasswordById(String id);

    // 插入用户
    @Insert("insert into reader(RNo,RName,ReaderPassword,RSex,ReaderPermission,ReaderPhoneNumber,ReaderAddress) values(#{RNo},#{RName},#{ReaderPassword},#{RSex},#{ReaderPermission},#{ReaderPhoneNumber},#{ReaderAddress})")
    void saveReader(User user);

    //查询用户的借书历史
    @Select("select book.BNo,BName,Bauthor,BKName,Bpublic,position,borrowdate,returndate from book,bookkind,borrowhistory where RNo= #{token} and borrowhistory.BNo=book.BNo and book.BKNo=bookkind.BKNo")
    ArrayList<Bookhistory> getBoorowHistory(String token);

    //续借操作
    @Select("select shouldreturndate from borrowandreturn where RNO=#{token} and BNo =#{book_id}")
    String getShouldreturndate(@Param("token") String token,@Param("book_id")long book_id);
    @Update("update borrowandreturn set shouldreturndate =#{shouldreturndate} where RNO=#{token} and BNo =#{book_id}")
    void setshouldreturndate(@Param("token") String token,@Param("book_id")long book_id,@Param("shouldreturndate")String shouldreturndate);

    //预约操作
    @Insert("insert into appoinment(RNo,BNo,AppDate) values(#{token},#{book_id},#{AppDate})")
    void insertAppDate(@Param("token")String token,@Param("book_id")long book_id,@Param("AppDate")String AppDate);

    //预约状态查询
    @Select("select book.BNo,BName,Bauthor,BKName,Bpublic,position,AppDate from book,appoinment,bookkind where appoinment.RNo=#{id} and appoinment.BNo=book.BNo and book.BKNo=bookkind.BKNo")
    ArrayList<Bookhistory>getAppDate(String id);

    //查询用户详细信息
    @Select("select RNo,RName,ReaderPhonenumber,RSex,ReaderAddress from reader where RNo=#{token}")
    ArrayList<Userinfo>getUserInfo(String token);

    // 查询用户未归还的图书
    @Select("select book.BNo,BName,Bauthor,BKName,Bpublic,position,borrowdate,shouldreturndate,reborrow_state from book,bookkind,borrowandreturn " +
            "where RNo=#{token} and book.BKNo=bookkind.BKNo and book.BNo=borrowandreturn.BNo and isreturnbook=0")
    ArrayList<BorrowBook> getBorrowBook(String token);

    // 更新图书得预约状态
    @Select("update book set isappoinment=0 where BNo=#{BNo}")
    void  updateBookIsAppinment(int BNo);

}
